﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI;
using Microsoft.Practices.Unity;
using Baosight.ZNH.ZMCB.UI.Utils;
using System.Linq;

namespace Baosight.ZNH.ZMCB.UI
{
    /// <summary>
    /// 巷道修改  
    /// </summary>
    /// Title:ZMCB07
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 高强
    /// Version: 1.0
    /// History:
    ///       2013-07-18  高强   [创建]
    ///
    public partial class ZMCB07 : EFViewCmes
    {
        public ZMCB07()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 点击“修改”按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            Button but = sender as Button;
            but.IsEnabled = false;
            base.TempButton = but;
            if (!this.UpdateGrid.ValidateField())
            {
                this.ValidateMessagePopup();
                base.TempButtonOpen(true);
                return;
            }
            else
            {
                //构造EIInfo
                //根据所属区域类型和巷道用途来设置所属工序
                CoalseamOfProcessJudge coalOfProcJudge = new CoalseamOfProcessJudge();
                this.fdProcess.Value = coalOfProcJudge.JudgeProcess(this.fdParentType.Value.ToString(), this.fdRoadwayUsing.Value.ToString());
                EIInfo subInfo = new EIInfo();
                subInfo.AddBlock(EIConstants.ResultBlock).SetColumns(GetColumns());
                subInfo.Attributes.Add(EIConstants.ServiceNameKey, "ZMCB07");
                subInfo.Attributes.Add(EIConstants.MethodNameKey, "update");
                subInfo.GetBlock(EIConstants.ResultBlock).AddRow(GetRowValues());
                this.Submit(subInfo, SubmitCompleted);
            }
        }

        /// <summary>
        /// 确定按钮后的回调函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SubmitCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                this.OnCommitMessagePopup(e.EIInfo);
            }
            base.TempButtonOpen(true);

            this.UpdateHomePageData(new EIInfo());//发布数据
        }

        /// <summary>
        /// 重写基类方法：根据数据的改变更新主页面的数据
        /// </summary>
        /// <param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }

        /// <summary>
        ///获取DataFieldColumns，不带参数的
        /// </summary>
        public Dictionary<string, EIColumn> GetColumns()
        {
            Dictionary<string, EIColumn> columns = new Dictionary<string, EIColumn>();
            foreach (var _field in this.UpdateGrid.GetChildObjects<EFDataField>(null, true))
            {
                EIColumn onecolumn = new EIColumn();
                onecolumn.Name = _field.eBindingName.Substring(9);
                onecolumn.Desc = _field.eLabel as string;
                columns.Add(_field.eBindingName, onecolumn);
            }
            return columns;
        }

        /// <summary>
        ///获取行的值
        /// </summary>
        public EIRow GetRowValues()
        {
            EIRow row = new EIRow();
            foreach (var _field in this.UpdateGrid.GetChildObjects<EFDataField>(null, true))
            {
                row.Add(_field.eBindingName, _field.Value);
            }
            return row;
        }

        /// 剩余进尺随设计进尺而变化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdLength_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            fdSurplusLength.Value = fdLength.Value;
        }
    }
}
